#include <iostream>
#include <sw/redis++/redis.h>
#include <chrono>
#include <thread>
#include <sw/redis++/redis++.h>
#include <unordered_map>
#include "util.hpp"

using std::cout;
using std::endl;
using std::string;
using std::unordered_map;
using std::vector;

void test1(sw::redis::Redis &redis)
{
    cout << "get 和 set练习" << endl;

    redis.flushall();

    redis.set("key1", "111");
    redis.set("key2", "222");
    redis.set("key3", "333");
    redis.set("key4", "2333");

    auto value1 = redis.get("key1");
    if (value1)
        cout << "value1: " << value1.value() << endl;

    auto value2 = redis.get("key2");
    if (value2)
        cout << "value2: " << value2.value() << endl;

    auto value3 = redis.get("key3");
    if (value3)
        cout << "value3: " << value3.value() << endl;

    auto value4 = redis.get("key4");
    if (value4)
        cout << "value4: " << value4.value() << endl;

    auto value5 = redis.get("key5");
    if (value5)
        cout << "value5: " << value5.value() << endl;
}

void test2(sw::redis::Redis &redis)
{
    cout << "exists" << endl;
    redis.flushall();

    redis.set("key","2233");
    redis.set("key3","2233");
    auto ret = redis.exists("key");
    cout << ret << endl;

    ret = redis.exists("key2");
    cout << ret << endl;

    ret = redis.exists({"key","key2","key3"});
    cout << ret << endl;
}

void test3(sw::redis::Redis &redis)
{
    cout << "dle" << endl;
    redis.flushall();

    redis.set("key","2233");
    redis.set("key3","2233");
    redis.set("key4","2233");
    redis.set("key5","2233");
    redis.set("key2","2233");

    auto ret = redis.del("key");
    cout << ret << endl;
    
    ret = redis.del({"key2","key3","key4","key5","key6"});
    cout << ret << endl;

    ret = redis.exists("key2");
    cout << ret << endl;

}

void test4(sw::redis::Redis &redis)
{
    cout << "keys " << endl;
    redis.flushall();

    redis.set("key1","2233");
    redis.set("key3","2233");
    redis.set("key4","2233");
    redis.set("key5","2233");
    redis.set("key2","2233");

    vector<string> ret;
    auto it = std::back_inserter(ret);
    redis.keys("*", it);
    printContiner(ret);

}

void test5(sw::redis::Redis &redis)
{
    using namespace std::chrono_literals;
    cout << "expire " << endl;
    redis.flushall();

    redis.set("key1","2233");
    redis.expire("key1", 10s);

    std::this_thread::sleep_for(3s);

    auto retTime = redis.ttl("key1");
    cout << retTime << endl;

}

void test6(sw::redis::Redis &redis)
{
    cout << "type " << endl;
    redis.flushall();

    redis.set("key1","2233");
    string ret = redis.type("key1");
    cout << "key1: " << ret << endl;

    redis.lpush("key2", "6666");
    ret = redis.type("key2");
    cout << "key2: " << ret << endl;

    redis.hset("key3", "cfcf", "1111");
    ret = redis.type("key3");
    cout << "key3: " << ret << endl;

    redis.sadd("key4", "cf");
    ret = redis.type("key4");
    cout << "key4: " << ret << endl;

    redis.zadd("key5", "cfn", 6666);
    ret = redis.type("key5");
    cout << "key5: " << ret << endl;
}

int main()
{
    sw::redis::Redis redis("tcp://127.0.0.1:6379");
    // test1(redis);
    // test4(redis);
    // test5(redis);
    test6(redis);

    return 0;
}